/**
 * Created by yu_huan on 2018/4/9.
 */
define(function () {
    var self, userInfo;

    function azyy() {
        self = this;
        self.vue = null;
        self.productData = [];
        self.mData = {
            productSetItem: null
        };

        userInfo = app.userInfos.sysUser;
        self._listVue = {
            keyword: '',
            list: []
        };
        self._listVue.GWAK030 = userInfo.MOBILE;
        self.init();
    }

    azyy.prototype.initVue = function () {
        //设备列表
        self.vue = new Vue({
            el: document.getElementById('serveredPull'),
            data: self._listVue,
            methods: {
                open: function (index) {
                    app.openPage({
                        src: 'fwyy/fwyy-info.html',
                        argsKey: 'azyy-info',
                        args: self.vue.list[index]
                    });
                    return;
                },
                zhibao: function (value) {
                    if (value == 1) {
                        return '是';
                    } else if (value == 0) {
                        return '否';
                    }else if (value == 2) {
                        return '延保';
                    }
                },
                toSearch: function (event) {
                    var _v = self._listVue.GWAK030.trim();
                    self.vue.keyword = event.target.value;
                    if (_v.length > 0) {
                        var product = self.mData.productSetItem;
                        product.page = 0;
                        self.getData(product.refresh);
                    }
                },
                scanQRCode : function(){
                    app.scanQRCode(function (s) {
                        self._listVue.GWAK030 = s;
                        var product = self.mData.productSetItem;
                        product.page = 0;
                        self.getData(product.refresh);
                    });
                },
            }
        })

    }

    azyy.prototype.getData = function (refresh) {
        var vue = self.vue;
        var product = self.mData.productSetItem;
        app.ajax({
            xurl: "api/wx/azyy/getListData",
            xdata: {
                page: product.page+1,
                limit: product.limit,
                keyword: vue.keyword
            },
            cb: function (result) {
                if (result.ok) {
                    if (product.page == 0) {
                        vue.list.removeAll();
                    }
                    vue.list.pushArray(result.data.data);
                    if (product.page == 0) {
                        refresh.endPulldownToRefresh();
                        var _tip = $('#pullrefresh');
                        if (result.data.data.length == 0) {
                            app.showNoData(_tip, '您还没有产品...');
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(_tip);
                            refresh.refresh(true);
                        }
                    }
                    product.page += 1;
                    refresh.endPullupToRefresh(result.data.data.length < product.limit);
                }
            }
        })
    }

    azyy.prototype.init = function () {
        self.initVue();
        mui.ready(function () {

            var refreshData = function () {
                if (!self.mData.productSetItem) {
                    self.mData.productSetItem = {
                        page: 0,
                        limit: 15,
                        start: function () {
                            return (this.page) * this.limit;
                        }
                    };

                    self.mData.productSetItem.refresh = mui('#pullrefresh').pullRefresh({
                        down: {
                            callback: function () {
                                self.mData.productSetItem.page = 0;
                                self.getData(this);

                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(this);
                            }
                        }
                    });
                }
            };
            refreshData();

        });

    };

    return new azyy();

});